/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package game1;

import java.awt.*;
import java.awt.geom.*;
import java.util.LinkedList;
import java.lang.Math;
/**
 *
 * @author Юра
 */
public class Friends {
    protected Point2D place, target;
    protected  int r=6, distance = 100;
    protected  double vx=0, vy=0, rotate=3, rotate_speed=0.1, bullet_speed = 10;
    protected  Color color;
    protected  LinkedList<Shape> img = new LinkedList();
    public boolean friend = Boolean.TRUE;
    protected Graphics2D graph;
    protected int reload=0, reload_time=3, load=0;
    protected LinkedList<Dots> boollets = new LinkedList();


    Friends(){
        this.place = new Point(100, 100);
        this.color = Color.WHITE;
        uniqDraw();
    }
    
    Friends(int x, int y){
        this.place = new Point(x, y);
        this.color = Color.WHITE;
        uniqDraw();
    }

    Friends(int x, int y, Color color){
        this.place= new Point(x, y);
        this.color = color;
        uniqDraw();
    }

    public void setColor(Color c) {
        this.color = c;
    }

    public void rotate(double a){
        this.rotate = a;
    }
    
    public void aim(Point2D p){
        double a = Math.atan2((p.getY()-this.place.getY()), (p.getX()-this.place.getX()));
        double dif = (rotate-a);

        a = (Math.abs(dif)>rotate_speed)?rotate_speed:Math.abs(dif);
        dif = (dif>Math.PI || dif<-Math.PI)?-dif:dif;
        if (dif<0)
        {
          this.rotate += a;
          rotate = rotate>Math.PI?rotate-2*Math.PI:rotate;
        }
        if (dif>0)
        {
          this.rotate -= a;
          if (rotate<(-Math.PI)) rotate+=2*Math.PI;
        }
        
        if (reload >0)
            reload--;
        else
            if (Math.abs(dif)<rotate_speed && this.place.distance(p)<this.distance)
            {
                this.shot();
                reload = reload_time;
            }
        //System.out.println(reload+"_"+place.distance(p));
    }

     public void uniqDraw() {
        img.add(new Rectangle2D.Double(this.place.getX()-r,this.place.getY()-r+this.r-1,this.r*2+3,2));
        img.add(new Ellipse2D.Double(this.place.getX()-r, this.place.getY()-r, this.r*2, this.r*2));
    }
    
    public void draw(Graphics2D g2) {
      // uniqDraw();
      g2.setStroke(new BasicStroke(1));
      g2.setColor(color);

      AffineTransform at = AffineTransform.getRotateInstance(rotate,this.place.getX(),this.place.getY());

        for ( Shape shape: this.img )
            {
            //Shape s = (Shape) img.pop();
            g2.draw(at.createTransformedShape(shape)) ;
            }
    }

    public void settarget(Point p) {
        this.target = p;
    }

    public void move() {
        place.setLocation(place.getX()+vx, place.getY()+vy);
        this.aim(target);
        movedots();
    }

    protected  void shot() {
        new AePlayWave("c:/Documents and Settings/Юра/Мои документы/NetBeansProjects/Game1/src/game1/Ak47.wav").start();
        boollets.add(new Dots(place));
        load++;
    }

    private void movedots() {
        for(Dots dot:boollets)
        {
            if (!dot.armed && load>0)
            {
                dot.armed=Boolean.TRUE;
                dot.shotto(rotate, bullet_speed);
                load--;
            }
            dot.move();
        }
    }
}



